package com.dynamic.idea.quick.develop.plugin.utils;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import org.apache.commons.lang3.StringUtils;

public class PsiClassUtils {

    /**
     * 判断类里是否存在某个属性
     * @param psiClass
     * @param fieldName
     * @return
     */
    public static PsiField getField(PsiClass psiClass,String fieldName){
        if (psiClass==null){
            return null;
        }
        PsiField[] fields = psiClass.getFields();

        if (fields==null||fields.length==0){
            return null;
        }

        for (PsiField psiField: fields){
            if (psiField==null){
                continue;
            }

            String name = psiField.getName();
            if (StringUtils.equals(StringUtils.trimToEmpty(name),StringUtils.trimToEmpty(fieldName))){
                return psiField;
            }
        }
        return null;
    }

    /**
     * 根据属性名 删除属性
     * @return
     */
    public static void removeField(PsiClass psiClass,String fieldName){
        if (psiClass==null){
            return ;
        }
        PsiField[] fields = psiClass.getFields();

        if (fields==null||fields.length==0){
            return;
        }
        for (PsiField psiField:fields){
            if (psiField==null){
                continue;
            }

            if (psiField instanceof PsiEnumConstant){
                continue;
            }

            String name = psiField.getName();
            if (StringUtils.equals(StringUtils.trimToEmpty(name),StringUtils.trimToEmpty(fieldName))){
                psiField.delete();
            }
        }
    }
    /**
     * 根据方法名删除方法
     * @return
     */
    public static void removeMethod(PsiClass psiClass,String methodName){
        if (psiClass==null){
            return ;
        }
        PsiMethod[] methods = psiClass.getMethods();

        if (methods==null||methods.length==0){
            return;
        }
        for (PsiMethod psiMethod:methods){
            if (psiMethod==null){
                continue;
            }

            String name = psiMethod.getName();
            if (StringUtils.equals(StringUtils.trimToEmpty(name),StringUtils.trimToEmpty(methodName))){
                psiMethod.delete();
            }
        }
    }

    /**
     * 判断类里是否存在某个属性
     * @param psiClass
     * @param fieldName
     * @return
     */
    public static boolean containField(PsiClass psiClass,String fieldName){
        if (psiClass==null){
            return false;
        }
        PsiField[] fields = psiClass.getFields();

        if (fields==null||fields.length==0){
            return false;
        }

        for (PsiField psiField: fields){
            if (psiField==null){
                continue;
            }

            if (psiField instanceof PsiEnumConstant){
                continue;
            }

            String name = psiField.getName();
            if (StringUtils.equals(StringUtils.trimToEmpty(name),StringUtils.trimToEmpty(fieldName))){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断类里是否存在某个方法
     * @param psiClass
     * @param methodName
     * @return
     */
    public static boolean containMethod(PsiClass psiClass, String methodName){
        if (psiClass==null){
            return false;
        }

        PsiMethod[] methods = psiClass.getMethods();
        if (methods==null||methods.length==0){
            return false;
        }

        for (PsiMethod psiMethod: methods){
            if (psiMethod==null){
                continue;
            }

            String name = psiMethod.getName();
            if (StringUtils.equals(StringUtils.trimToEmpty(name),StringUtils.trimToEmpty(methodName))){
                return true;
            }
        }

        return false;

    }

    /**
     * 格式化导入java代码
     * @param cls
     */
    public static void formatJavaCode(PsiClass cls) {
        if (cls == null) {
            return;
        }
        JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(cls.getProject());
        styleManager.optimizeImports(cls.getContainingFile());
        styleManager.shortenClassReferences(cls);
    }

}
